package cz.tsystems.grids;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import com.fasterxml.jackson.databind.JsonNode;

import cz.tsystems.data.DataProvider;
import cz.tsystems.portablecheckin.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;

public class PlanZakazkyActivity extends Activity {

	public static enum eGrdTypes {eGRDPLANZAK};
	private TableLayout grdTable;
	private TableRow.LayoutParams rowParams;
//	private List<TextView> txtViews;
	private eGrdTypes type; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_grd_plan_zakazky);
		type = (eGrdTypes) getIntent().getExtras().getSerializable("type");
		
		grdTable = (TableLayout) findViewById(R.id.gridTable);
		rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
		rowParams.span = 3;
        rowParams.leftMargin = 5;
        rowParams.rightMargin = 5;
        rowParams.topMargin = 5;
        rowParams.bottomMargin = 5;
        
        loadColumns();
	}
	
	private void loadColumns()
	{
		switch (type) {
		case eGRDPLANZAK:
//			txtViews = GrdColumnsSettings.getPlanZakazkaCS(this);
			buildPlanZak();
			break;

		default:
			break;
		}
	}
	
//	public void addColumn(TextView txtView)
//	{
//		if(txtViews == null)
//			txtViews = new ArrayList<TextView>();
//		
//		txtViews.add(txtView);
//	}
//	
//	public TextView getColumn(int i)
//	{
//		return txtViews.get(i);
//	}
	
	public void buildRows()
	{
		switch (type) {
		case eGRDPLANZAK:
			buildPlanZak();
			break;

		default:
			break;
		}
	}
	
	private void buildPlanZak()
	{
		JsonNode data = DataProvider.getSharedDataProvider().getPlanZakazk();
		
		if(data == null)
			return;
		
		Iterator<JsonNode> ijn = data.path("CHECKIN_ORDER_LIST").elements();
		
		while (ijn.hasNext()) {
		
			Iterator<JsonNode> fields = ijn.next().elements();
			TableRow theRow = new TableRow(this);			
//			theRow.setBackgroundColor(0xFF0000FF);
			List<TextView> tvs = GrdColumnsSettings.getPlanZakazkaCS(this);
			int i = 0;			
			while(fields.hasNext())
			{
				if(i >= tvs.size())
					break;				
				TextView tv = tvs.get(i);
				JsonNode field = fields.next();
				Log.d("fieldsName", field.asText());
				tv.setText(field.asText());
//				if(field.isTextual())					
//					tv.setText(field.textValue());
//				else if(field.isNumber())
//					tv.setText(String.valueOf(field.intValue()));
//				else if(field.isNull())
//					tv.setText(".......");
				
				theRow.addView(tv, rowParams);
				
//				if(field.isTextual())
//					Log.d("fields", field.textValue());
				i++;
			}
			grdTable.addView(theRow, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		}		
	}

	public eGrdTypes getType() {
		return type;
	}

	public void setType(eGrdTypes type) {
		this.type = type;
	}
}
